package com.anfeng.cms.service.impl.emp;

import com.anfeng.cms.common.Common;
import com.anfeng.cms.domain.emp.AfPhotoImpLog;
import com.anfeng.cms.repository.mysql.dao.emp.AfPhotoImpLogMapper;
import com.anfeng.cms.service.emp.IAfPhotoImpLogService;
import com.anfeng.cms.util.IdGeneratorUtil;
import com.anfeng.cms.util.JsonMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴朋
 * @since 2019-07-15
 */
@Service
@Slf4j
public class AfPhotoImpLogServiceImpl extends ServiceImpl<AfPhotoImpLogMapper, AfPhotoImpLog> implements IAfPhotoImpLogService {


    /**
     * 清空失败信息
     *
     * @return
     */
    @Override
    public int clearUploadImageFailureInfo() {
        List<AfPhotoImpLog> failedImpLogs = this.getAllFailedImpLogs();
        if (null == failedImpLogs || failedImpLogs.isEmpty()) {
            log.info("清空导入失败信息 但是未获取到相关失败信息");
            return Common.RETURN_SUCCESS;
        }
        for (AfPhotoImpLog impLog : failedImpLogs) {
            impLog.setState(Common.DATA_STATE_DELETE);
            impLog.setModifiedTime(new Date());
        }
        boolean result = this.updateBatchById(failedImpLogs);
        if (!result) {
            log.error("清空上传失败信息 失败 清空数据数量 {}", failedImpLogs.size());
            return Common.RETURN_FAILED;
        }
        return Common.RETURN_SUCCESS;
    }

    /**
     * 获取所有导入错误的图片信息
     *
     * @return 导入失败的图片信息
     */
    @Override
    public List<AfPhotoImpLog> getAllFailedImpLogs() {
        EntityWrapper<AfPhotoImpLog> wrapper = this.generatorPhotoImpLogWrapper(Common.UPLOAD_IMAGE_FAILED);
        return this.selectList(wrapper);
    }

    /**
     * 获取所有导入失败的记录数量
     *
     * @return 数量
     */
    @Override
    public int getAllFailedImpLogCount() {
        EntityWrapper<AfPhotoImpLog> wrapper = this.generatorPhotoImpLogWrapper(Common.UPLOAD_IMAGE_FAILED);
        return this.selectCount(wrapper);
    }

    /**
     * 新增上传日志
     *
     * @param photoImpLog 日志信息
     * @return 日志信息
     */
    @Override
    public String addPhotoImpLog(AfPhotoImpLog photoImpLog) {
        if (null == photoImpLog) {
            log.error("导入图片上传日志失败 传入参数为空");
            return null;
        }
        String logId = IdGeneratorUtil.uuid();
        photoImpLog.setId(logId);
        photoImpLog.setState(Common.DATA_STATE_NORMAL);
        Date currentDate = new Date();
        photoImpLog.setCreateTime(currentDate);
        photoImpLog.setModifiedTime(currentDate);

        boolean insert = this.insert(photoImpLog);
        if (!insert) {
            log.error("新增导入图片日志失败 {}", JsonMapper.defaultMapper().toJson(photoImpLog));
            return null;
        }
        return logId;
    }

    /**
     * 生成获取是否成功的wrapper
     * @param success 是否成功 1 成功 2 失败
     * @return wrapper
     */
    private EntityWrapper<AfPhotoImpLog> generatorPhotoImpLogWrapper(Integer success) {
        AfPhotoImpLog photoImpLog = new AfPhotoImpLog();
        photoImpLog.setSuccess(success);
        photoImpLog.setState(Common.DATA_STATE_NORMAL);
        EntityWrapper<AfPhotoImpLog> wrapper = new EntityWrapper<>();
        wrapper.setEntity(photoImpLog);
        return wrapper;
    }
}
